package com.hong.demo.other.fsm;

import com.hong.demo.other.fsm.impl.OrderEvent;
import com.hong.demo.other.fsm.impl.OrderFSMContext;
import com.hong.demo.other.fsm.impl.OrderFSMTransitionBlock;
import com.hong.demo.other.fsm.impl.OrderState;

//@Service
public class MainTest {

    //@Resource
    private StateMachine stateMachine;

    public MainTest(StateMachine stateMachine) {
        this.stateMachine = stateMachine;
    }

    public static void main(String[] args) {
        StateMachine stateMachine = new StateMachine();
        stateMachine.setName("订单状态机");
        stateMachine.setIfsmTransitionBlock(new OrderFSMTransitionBlock());
        MainTest mainTest = new MainTest(stateMachine);

        Order order = new Order();
        order.setState(OrderState.WAIT_PAY);
        mainTest.userPay(order);
    }

    /**
     * 订单支付
     */
    public void userPay(Order order) {
        OrderFSMContext context = new OrderFSMContext();
        context.setSourceState(order.getState());
        context.setEvent(OrderEvent.PAY);
        context.setTargetState(OrderState.PAYED);
        context.setOrder(order);

        stateMachine.fire(context);
    }
}
